package com.sunshine.homepage.controller;

import com.sunshine.homepage.entity.Products;
import com.sunshine.homepage.entity.ShoppingCarts;
import com.sunshine.homepage.service.ShoppingCartService;
import com.sunshine.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import sunshine.commcon.util.entity.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingcart")
public class ShoppingCartController {
    
    @Resource
    private ShoppingCartService shoppingCartService;
    
    /**
     * 添加商品到购物车
     * @param request HTTP请求，用于从token中提取用户ID
     * @param cartRequest 购物车请求数据
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result<Boolean> addProductToCart(
            HttpServletRequest request,
            @RequestBody CartRequest cartRequest) {
        
        // 从token中提取用户ID
        Long userId = TokenUtil.getUserIdFromRequest(request);
        if (userId == null) {
            return Result.fail("用户未登录或token无效");
        }
        
        Long productId = cartRequest.getProductId();
        Integer quantity = cartRequest.getQuantity();
        
        log.info("用户添加商品到购物车: 用户ID={}, 商品ID={}, 数量={}", userId, productId, quantity);
        
        // 调用服务添加商品到购物车
        return shoppingCartService.addProductToCart(userId, productId, quantity);
    }
    
    /**
     * 获取用户的购物车商品列表
     * @param request HTTP请求，用于从token中提取用户ID
     * @return 购物车商品列表
     */
    @GetMapping("/items")
    public Result<List<ShoppingCartVO>> getCartItems(HttpServletRequest request) {
        // 从token中提取用户ID
        Long userId = TokenUtil.getUserIdFromRequest(request);
        if (userId == null) {
            return Result.fail("用户未登录或token无效");
        }
        
        log.info("用户获取购物车商品列表: 用户ID={}", userId);
        
        // 调用服务获取购物车商品列表
        return shoppingCartService.getCartItems(userId);
    }
    
    /**
     * 更新购物车商品数量
     * @param request HTTP请求，用于从token中提取用户ID
     * @param cartId 购物车项ID
     * @param updateRequest 更新请求数据
     * @return 操作结果
     */
    @PostMapping("/update/{cartId}")
    public Result<Boolean> updateCartItem(
            HttpServletRequest request,
            @PathVariable Long cartId,
            @RequestBody UpdateCartRequest updateRequest) {
        
        // 从token中提取用户ID
        Long userId = TokenUtil.getUserIdFromRequest(request);
        if (userId == null) {
            return Result.fail("用户未登录或token无效");
        }
        
        Integer quantity = updateRequest.getQuantity();
        
        log.info("用户更新购物车商品数量: 用户ID={}, 购物车项ID={}, 数量={}", userId, cartId, quantity);
        
        // 调用服务更新购物车商品数量
        return shoppingCartService.updateCartItem(userId, cartId, quantity);
    }
    
    /**
     * 删除购物车商品
     * @param request HTTP请求，用于从token中提取用户ID
     * @param cartId 购物车项ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{cartId}")
    public Result<Boolean> deleteCartItem(
            HttpServletRequest request,
            @PathVariable Long cartId) {
        
        // 从token中提取用户ID
        Long userId = TokenUtil.getUserIdFromRequest(request);
        if (userId == null) {
            return Result.fail("用户未登录或token无效");
        }
        
        log.info("用户删除购物车商品: 用户ID={}, 购物车项ID={}", userId, cartId);
        
        // 调用服务删除购物车商品
        return shoppingCartService.deleteCartItem(userId, cartId);
    }
    
    /**
     * 购物车请求数据载体
     */
    public static class CartRequest {
        private Long productId;
        private Integer quantity = 1;
        
        public Long getProductId() {
            return productId;
        }
        
        public void setProductId(Long productId) {
            this.productId = productId;
        }
        
        public Integer getQuantity() {
            return quantity;
        }
        
        public void setQuantity(Integer quantity) {
            this.quantity = quantity;
        }
    }
    
    /**
     * 更新购物车请求数据载体
     */
    public static class UpdateCartRequest {
        private Integer quantity;
        
        public Integer getQuantity() {
            return quantity;
        }
        
        public void setQuantity(Integer quantity) {
            this.quantity = quantity;
        }
    }
    
    /**
     * 购物车VO（View Object）用于返回给前端
     */
    public static class ShoppingCartVO {
        private Long cartId;
        private Long productId;
        private Integer quantity;
        private Products product;
        
        public Long getCartId() {
            return cartId;
        }
        
        public void setCartId(Long cartId) {
            this.cartId = cartId;
        }
        
        public Long getProductId() {
            return productId;
        }
        
        public void setProductId(Long productId) {
            this.productId = productId;
        }
        
        public Integer getQuantity() {
            return quantity;
        }
        
        public void setQuantity(Integer quantity) {
            this.quantity = quantity;
        }
        
        public Products getProduct() {
            return product;
        }
        
        public void setProduct(Products product) {
            this.product = product;
        }
    }
}